﻿Public Class frmTaxCodeBR
    Private frmParent As frmMain
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmTaxCodeBR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = frmMain
        ClearAllFields()
        Try
            'Dim taxcodeList As List(Of TaxCode) = sqlList.getAllTaxCode()
            Dim taxcodeList As List(Of TaxCode) = payrollManager.getAllTaxCode()
            For Each taxcode As TaxCode In taxcodeList
                Dim item As ListViewItem
                item = lvwTaxCode.Items.Add(taxcode.tax_code)
                item.SubItems.Add(taxcode.tax_code_desc)
                item.SubItems.Add(taxcode.exemption)
                item.SubItems.Add(taxcode.id)   
            Next
            grpTaxCode.Text = "Total Record(s): " + lvwTaxCode.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpTaxCode)
        btnAdd.Enabled = True
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        grpTaxCode.Text = "Total Record(s): 0"
        validator.resizes(lvwTaxCode)
        Clipboard.Clear()
        Return 0
    End Function

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        frmTaxCodeDE.ShowDialog()
        frmTaxCodeBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub lvwTaxCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwTaxCode.SelectedIndexChanged
        btnEdit.Enabled = (lvwTaxCode.SelectedItems.Count = 1)
        btnDelete.Enabled = (lvwTaxCode.SelectedItems.Count > 0)
    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmImport.ShowDialog()
        frmTaxCodeBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Clipboard.SetText(lvwTaxCode.SelectedItems(0).SubItems(3).Text)
        frmTaxCodeDE.ShowDialog()
        frmTaxCodeBR_Load(vbNull, EventArgs.Empty)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            For i As Integer = 1 To lvwTaxCode.SelectedItems.Count
                'sqlManager.executeQuery("DELETE FROM ref_tax_code WHERE id = " + lvwTaxCode.SelectedItems(i - 1).SubItems(3).Text)
                payrollManager.deleteItemTaxCode(lvwTaxCode.SelectedItems(i - 1).SubItems(3).Text)
            Next
            MsgBox("Record(s) has been deleted.")
            frmTaxCodeBR_Load(vbNull, EventArgs.Empty)
            grpTaxCode.Text = "Total Record(s): " + lvwTaxCode.Items.Count.ToString()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
    End Sub
End Class